<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Model\Admin;

use App\Model\Model;
use Carbon\Carbon;

/**
 * @property int $id ID
 * @property string $username 用户名
 * @property string $nickname 昵称
 * @property string $password 密码
 * @property string $avatar 头像
 * @property string $email 邮箱
 * @property string $mobile 手机
 * @property string $login_at 登录时间
 * @property int $status 帐号状态（0正常 1停用）
 * @property int $sex 用户性别（0男 1女 2未知）
 * @property int $del_flag 删除标志（0代表存在 2代表删除）
 * @property string $login_ip 最后登录IP
 * @property string $login_date 最后登录时间
 * @property string $remark 备注
 * @property int $created_by 创建者
 * @property Carbon $created_at 创建时间
 * @property Carbon $updated_at 更新时间
 * @property int $updated_by 修改人
 * @property string $deleted_at 删除时间
 * @property int $dept_id 部门id
 */
class Admin extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'admins';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'username', 'nickname', 'password', 'avatar', 'email', 'mobile', 'login_at', 'status', 'sex', 'del_flag', 'login_ip', 'login_date', 'remark', 'created_by', 'created_at', 'updated_at', 'updated_by', 'deleted_at', 'dept_id'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'status' => 'integer', 'sex' => 'integer', 'del_flag' => 'integer', 'created_by' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'updated_by' => 'integer', 'dept_id' => 'integer'];

    protected array $appends = [
        'role_key',
    ];

    public function getRoleKeyAttribute($value): string
    {
        $role_key = 'null';
        $user_id = $this->id;
        if ($user_id) {
            $role_id = AdminRole::where('admin_id', $user_id)->value('role_id');
            if ($role_id) {
                $role_key = Role::where('id', $role_id)->value('role_key') ?? 'null';
            }
        }
        return $role_key;
    }
}
